(function($, doc, room) {
	mui.init();
	var settings = app.getSettings();
	var state = app.getState();
	var uid = state.user_id;
	var cdata = sysphone.getFriendPin();
	//console.log(JSON.stringify(cdata));
	//var qdata = JSON.parse(localStorage.getItem('qunlist' + uid)) || [];
	doc.querySelector("#list-area").innerHTML = template('list-template', {
		"flist": cdata
	});
	var list = document.getElementById('list');
	var header = document.querySelector('.mui-bar');
	var bodyheight = document.body.offsetHeight;
	//calc hieght
	list.style.height = (bodyheight - 80) + 'px';
	//doc.querySelector('.mui-indexed-list-bar').style.height = bodyheight;
	//create
	window.indexedList = new mui.IndexedList(list);
	doc.querySelector('#addbtn').addEventListener('tap', function() {
		//var cks = doc.querySelector(".cks");
		var ids = [];
		var names = [];
		var heads = [];
		$(".cks").each(function(i, el) {
			if(el.checked == true) {
				ids.push(el.getAttribute('fid'));
				names.push(el.getAttribute('title'));
				heads.push(el.getAttribute('head'));
			}
		});
		//console.log(ids);
		//console.log(names);
		//return;
		if(ids.length > 1) {
			var postdata = {
				user_id: uid,
				ids: ids.join(","),
				names: names.join(","),
				heads: heads.join(",")
			};
			console.log(JSON.stringify(postdata));
			$.post(apiurl + 'chat/addRoom', postdata, function(res) {
				console.log(JSON.stringify(res));

				if(res.status > 0) {
					room.add(res);
					//qdata.push(res);
					//localStorage.setItem('qunlist' + uid, JSON.stringify(qdata));
					mui.openWindow({
						url: '/pages/chat/chat_index.html',
						id: '/pages/chat/chat_index.html',

						show: {
							autoShow: true,
							aniShow: 'slide-in-right',
							duration: 100
						},
						extras: {
							chatTitle: res.info.name,
							chatHeader: '../../static/images/qun.png',
							roomId: res.room_id,
							roomType: 2,
							tomsgtype: 'tip',
							tomsgsender: 'sys',
							tomsgcontent: '你邀请' + names.join("、") + '加入群聊'
							
						}
					});
				} else {
					$.alert("创建失败！");
				}
			});
		} else {
			if(ids.length > 0) {
				mui.openWindow({
					url: '/pages/chat/chat_index.html',
					id: '/pages/chat/chat_index.html',

					show: {
						autoShow: true,
						aniShow: 'slide-in-right',
						duration: 100
					},
					extras: {
						chatTitle: names[0],
						chatHeader: heads[0],
						roomId: ids[0],
						roomType: 1
					}
				});
			} else {
				$.alert("请选择群成员！");
			}

		}

		console.log(names);

	});

}(mui, document, room));